package com.cabrida.livestar.mainstar;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.cabrida.livestar.R;
import com.cabrida.livestar.config.Global;
import com.cabrida.livestar.livestar.StarLiveRoomAdvanceActivity;
import com.cabrida.livestar.mainstar.setlive.SetLiveActivity;
import com.cabrida.livestar.message.MessageActivity;
import com.cabrida.livestar.model.StarMainData;
import com.cabrida.livestar.util.MyUtils;
import com.cabrida.livestar.view.CircleImageView;

/**
 * 明星端首页
 * 
 * @author why
 */
public class StarHomePageView extends LinearLayout implements OnClickListener {
	private Context mContext;
	private Button btn_setlive;
	private TextView tv_empty;
	private CircleImageView iv_header;
	private TextView tv_name;

	private ListView listView;

	private ImageView iv_msg;
	private StarHomePageLvAdapter lvAdapter;
	
	private Button btn_live_begin;

	public StarHomePageView(Context context) {
		super(context);
		mContext = context;
		View view = LayoutInflater.from(context).inflate(
				R.layout.view_main_star_homepage, this);
		Init(view);
	}

	/**
	 * 初始化控件
	 */
	private void Init(View view) {
		listView = (ListView) view.findViewById(R.id.listView);
		iv_msg = (ImageView) view.findViewById(R.id.iv_msg);
		iv_msg.setOnClickListener(this);
		tv_empty = (TextView) view.findViewById(R.id.tv_empty);
		btn_setlive = (Button) view.findViewById(R.id.btn_setlive);
		btn_setlive.setOnClickListener(this);

		lvAdapter = new StarHomePageLvAdapter(mContext);
		listView.setAdapter(lvAdapter);
		
		btn_live_begin = (Button) view.findViewById(R.id.btn_live_begin);
		btn_live_begin.setOnClickListener(this);

		iv_header = (CircleImageView) findViewById(R.id.iv_header);
		tv_name = (TextView) findViewById(R.id.tv_name);
		MyUtils.loadImg(Global.headPic, iv_header);
		tv_name.setText(Global.S_name);
		
		addOption();

	}

	/**
	 * 添加时间轴
	 */
	private void addOption() {
		Intent intent = new Intent();
		intent.setAction(MainStarActivity.ACTION_MAINSTAR);
		mContext.sendBroadcast(intent);
	}
	
	public void notifyStarData(ArrayList<StarMainData> infoList){
		if (null==infoList) {
			return;
		}
		if (infoList.size()>0) {
			tv_empty.setVisibility(View.GONE);
		}else {
			tv_empty.setVisibility(View.VISIBLE);
		}
		lvAdapter.setData(infoList);
		lvAdapter.notifyDataSetChanged();
	}

	@Override
	public void onClick(View v) {
		Intent intent = null;
		switch (v.getId()) {
		case R.id.iv_msg:
			intent = new Intent();
			intent.setClass(mContext, MessageActivity.class);
			intent.putExtra("type", Global.TYPE_STAR);
			mContext.startActivity(intent);
			break;
		case R.id.btn_setlive:
			intent = new Intent();
			intent.setClass(mContext, SetLiveActivity.class);
			mContext.startActivity(intent);
			break;
		case R.id.btn_live_begin:
			intent = new Intent();
			intent.setClass(mContext, StarLiveRoomAdvanceActivity.class);
			mContext.startActivity(intent);
			break;
		default:
			break;
		}
	}

}
